import $ from './jquery.min.js';
$(() => {
    // 新增类型
    $("#addHeadTeachersBtn").on('click', function () {
        $.ajax({
            url: '/Types',
            data: {
                typename: $("#addHeadTeachersName").val()
            },
            type: 'post',
            success({ message, status }) {
                if (status == 200) {
                    alert(message);
                } else {
                    alert(message);
                }
            }
        })
    })
    // 类型列表
    $('dd .typesList').click(function () {
        $.ajax({
            url: '/Types',
            success({ message, status, types }) {
                typeRender(types);
            }
        })
    })

    $("#typeTb").on('click', 'a', function () {
        const typename = $(this).parents('tr').find('.typename').text();
        const result = confirm(`你确定要删除 ${typename} 吗？`);
        if (result) {
            const typeId = $(this).attr("data-typeId")
            $.ajax({
                url: `/Types/${typeId}`,
                type: 'delete',
                success({ message, status, types }) {
                    alert("删除成功")
                    typeRender(types)
                }
            })
        }
    })
})

function typeRender(data) {
    $("#typeTb").html('');
    let result = data.map(type =>
        `<tr>
            <td>${type._id}</td>
            <td class="typename">${type.typename}</td>
            <td>
                <a href="javascript:;" data-typeId="${type._id}">删除</a>
            </td>
        </tr>`
    );
    $("#typeTb").html(result);
}